package com.undergroundbiology.undergroundbiology;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;


public class Gallery extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);


        final WebView webview = (WebView)findViewById(R.id.webView);
        webview.getSettings().setJavaScriptEnabled(true);
        WebSettings webSettings = webview.getSettings();
        webview.getSettings().setBuiltInZoomControls(true);
        webview.getSettings().setAppCacheEnabled(true);
        webview.getSettings().setSaveFormData(true);
        webview.getSettings().setAllowFileAccess(true);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(true);


        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                webview.loadUrl("javascript:(function() { " +
                        "document.getElementsByClassName('fa-bars')[0].style.display='none'; " +
                        "})()");
                webview.loadUrl("javascript:(function() { " +
                        "document.getElementsByClassName('fa')[0].style.display='none'; " +
                        "})()");
                webview.loadUrl("javascript:(function() { " +
                        "document.getElementsByClassName('sidebar-toggle')[0].style.display='none'; " +
                        "})()");
                webview.loadUrl("javascript:(function() { " +
                        "document.getElementsByClassName('site-branding')[0].style.display='none'; " +
                        "})()");
            }
        });

        webview.loadUrl("http://www.TrinovaLabs.com/?page_id=54");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_gallery, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.refresh_settings:
                WebView myWebView = (WebView) findViewById(R.id.webView);
                myWebView.setWebViewClient(new WebViewClient());
                myWebView.reload();
                Toast.makeText(this, R.string.status_refreshing, Toast.LENGTH_SHORT)
                        .show();
                return true;

            case R.id.menu_share:
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                WebView shareURL = (WebView) findViewById(R.id.webView);
                String shareBody = shareURL.getUrl();
                shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Underground Biology");
                shareIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
                startActivity(Intent.createChooser(shareIntent, "Share Via"));
                return true;

            case R.id.action_search:
                AlertDialog.Builder alert = new AlertDialog.Builder(this);

                alert.setTitle("Search");
                final EditText search_input = new EditText(this);
                alert.setView(search_input);

                alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String search_input_2 = search_input.getText().toString();
                        String searchConvert = TextUtils.htmlEncode(search_input_2);
                        WebView searchWebView = (WebView) findViewById(R.id.webView);
                        showToastSearching();
                        searchWebView.loadUrl("http://TrinovaLabs.com/?s=" + searchConvert);

                    }
                });

                alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Cancel the Search Dialog.
                    }
                });

                alert.show();
                return true;

            case android.R.id.home:
                Intent homeIntent = new Intent (this, MainMenu.class);
                startActivity(homeIntent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    // ShowToast for Searching
    public void showToastSearching() {
        Toast.makeText(this,R.string.status_searching, Toast.LENGTH_SHORT).show();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        WebView myWebView = (WebView) findViewById(R.id.webView);
        myWebView.setWebViewClient(new WebViewClient());


        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


}
